package com.lianniu.web.tea.pay.controller;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.tea.pay.service.ITeaPayService;
import com.lianniu.web.tea.token.util.WeixinUtil;
import com.lianniu.web.tea.user.info.model.TeaFrontusers;
import com.lianniu.web.tea.user.info.service.ITeaInfoService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/rest/tea/pay")
public class TeaPayController {

	private final static Logger logger = Logger.getLogger(TeaPayController.class);
	
	@Autowired
	private ITeaPayService teaPayService;
	@Autowired
	private ITeaInfoService teaInfoService;

	@RequestMapping(value="/balancePay", method = RequestMethod.GET)
	public @ResponseBody BaseResult balancePay(HttpServletRequest req){
		logger.info("TeaPayController  balancePay begin.");
		try {
			logger.info("balancePay begin.");
			String orderId = req.getParameter("orderId");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaPayService.balancePay(orderId,userId);
			logger.info("balancePay end.");
			logger.info("TeaPayController balancePay end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaPayController balancePay error:" + e.getMessage());
			logger.error("TeaPayController balancePay end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkBalancePay", method = RequestMethod.GET)
	public @ResponseBody BaseResult checkBalancePay(HttpServletRequest req){
		logger.info("TeaPayController  checkBalancePay begin.");
		try {
			logger.info("checkBalancePay begin.");
			String orderId = req.getParameter("orderId");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaPayService.checkBalancePay(orderId,userId);
			logger.info("checkBalancePay end.");
			logger.info("TeaPayController checkBalancePay end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaPayController checkBalancePay error:" + e.getMessage());
			logger.error("TeaPayController checkBalancePay end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	@RequestMapping(value="/checkPayPassword", method = RequestMethod.GET)
	public @ResponseBody BaseResult checkPayPassword(HttpServletRequest req){
		logger.info("TeaPayController  checkPayPassword begin.");
		try {
			logger.info("checkPayPassword begin.");
			String payPassword = req.getParameter("payPassword");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaPayService.checkPayPassword(payPassword,userId);
			logger.info("checkPayPassword end.");
			logger.info("TeaPayController checkPayPassword end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaPayController checkPayPassword error:" + e.getMessage());
			logger.error("TeaPayController checkPayPassword end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 查询余额的接口
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getBalance", method = RequestMethod.GET)
	public @ResponseBody BaseResult getBalance(HttpServletRequest req){
		logger.info("TeaPayController  getBalance begin.");
		try {
			logger.info("getBalance begin.");
			String userId = req.getParameter("userId");
			BaseResult baseResult = teaPayService.getBalance(userId);
			logger.info("getBalance end.");
			logger.info("TeaPayController getBalance end.");
			return baseResult;
		} catch (Exception e) {
			logger.error("TeaPayController getBalance error:" + e.getMessage());
			logger.error("TeaPayController getBalance end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	/**
	 * 查询余额的接口
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getOpenId", method = RequestMethod.GET)
	public @ResponseBody BaseResult getOpenId(HttpServletRequest req){
		logger.info("TeaPayController  getOpenId begin.");
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxb0f5fe89c5840bf4&secret=f5d9e0b78ca03c83d72323eded7b3876&code=codeReq&grant_type=authorization_code";
		try {
			logger.info("getOpenId begin.");
			String code = req.getParameter("code");
			url = url.replace("codeReq", code);
			//发送post请求到微信，获取当前用户的openId
			JSONObject jsonObject = WeixinUtil.httpRequest(url, "GET", null);
			// 如果请求成功
			String openid = "";
			if (null != jsonObject) {
				try {
					logger.info("jsonObject:"+jsonObject.toString());
					openid=jsonObject.getString("openid");
					logger.info("openid:"+openid);
				} catch (Exception e) {
					// 获取token失败
				}
			}
			logger.info("getOpenId end.");
			logger.info("TeaPayController getOpenId end.");
			return BaseResult.success(jsonObject.toString());
		} catch (Exception e) {
			logger.error("TeaPayController getOpenId error:" + e.getMessage());
			logger.error("TeaPayController getOpenId end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
	@RequestMapping(value="/changeBalance", method = RequestMethod.GET)
	public @ResponseBody BaseResult changeBalance(HttpServletRequest req){
		logger.info("TeaPayController  changeBalance begin.");
		try {
			logger.info("changeBalance begin.");
			String payPrice = req.getParameter("payPrice");
			String userId = req.getParameter("userId");
			 //给用户的零钱更加金额
            BaseResult teaInfo = teaInfoService.getTeaInfo(userId);
            TeaFrontusers frontusers = (TeaFrontusers) teaInfo.getData();
            BigDecimal balance = frontusers.getBalance();
            balance = balance.add(new BigDecimal(payPrice));
            TeaFrontusers teaFrontusers = new TeaFrontusers();
            teaFrontusers.setBalance(balance);
            teaFrontusers.setUsersid(Long.valueOf(userId));
            teaInfoService.updateWeixinForuser(teaFrontusers);
			
			logger.info("changeBalance end.");
			logger.info("TeaPayController changeBalance end.");
			return BaseResult.success("ok");
		} catch (Exception e) {
			logger.error("TeaPayController changeBalance error:" + e.getMessage());
			logger.error("TeaPayController changeBalance end.");
			return BaseResult.exception(e.getMessage());
		}
	}
	
	
}
